#region

using System.Linq.Expressions;
using Vin.Extension.Framework.Dto;
using Vin.Extension.Framework.Entities;
using Vin.Extension.JwtBearer.Models;
using Vin.Extension.SqlSugar.Repository;

#endregion

namespace Vin.Extension.Framework.Repository;

public enum SaveType
{
    /// <summary>
    /// 插入
    /// </summary>
    Insert,

    /// <summary>
    /// 更新
    /// </summary>
    Update,
}

public interface IVinFrameworkBaseRepository<TEntity> : IVinBaseLogicRepository<TEntity>
    where TEntity : VinFrameworkBaseEntity, new()
{
    #region 获取Token授权信息

    public VinJwtTokenModel? GetTokenModel();

    public T? GetTokenModel<T>() where T : VinJwtTokenModel;

    #endregion

    #region 查询

    public Task<List<TEntity>> QueryList(Expression<Func<TEntity, bool>>? whereExpression = null);

    /// <summary>
    /// 根据ID获取实体
    /// </summary>
    public Task<TEntity> QueryEntityByIdAsync(long? id, string errorMsg = "数据不存在");
    
    public Task<List<TEntity>> QueryByIdsAsync(List<long> ids);

    #endregion

    #region 保存或更新

    /// <summary>
    /// 设置操作人信息
    /// </summary>
    public void SetOperateInfo(TEntity entity);

    /// <summary>
    /// 保存或更新
    /// </summary>
    public Task<TEntity?> SaveOrUpdateAsync(TEntity entity, Func<TEntity, Task>? insertCallback,
        Func<TEntity, Task>? updateCallback = null,
        bool isIgnoreNull = true);

    public Task<TEntity?> SaveOrUpdateAsync(TEntity entity, bool isIgnoreNull = true);
    
    public Task<TEntity?> SaveOrUpdateAsync(TEntity entity, Func<SaveType, TEntity, Task>? saveCallback,
        bool isIgnoreNull = true);
    
    public Task<bool> InsertRangeAsync(List<TEntity> entities, bool isIgnoreNull);

    #endregion

    #region 删除

    public Task<bool> LogicDeleteAsync(long? id);

    #endregion
    
    #region 对比

    public (List<TEntity>, List<TEntity>, List<TEntity>) Compare(
        List<TEntity> newDtoList, List<TEntity> newList);

    public (List<TEntityDto>, List<TEntity>, List<TEntity>) Compare<TEntityDto>(
        List<TEntityDto> newDtoList, List<TEntity> newList)
        where TEntityDto : VinFrameworkBaseEntityDto;

    #endregion
}

public interface IVinFrameworkBaseRecordRepository<TEntity> : IVinBaseLogicRepository<TEntity>
    where TEntity : VinFrameworkBaseRecordEntity, new()
{
    #region 获取Token授权信息

    public VinJwtTokenModel? GetTokenModel();

    public T? GetTokenModel<T>() where T : VinJwtTokenModel;

    #endregion

    #region 查询

    public Task<List<TEntity>> QueryListAsync(Expression<Func<TEntity, bool>>? whereExpression = null);

    /// <summary>
    /// 根据ID获取实体
    /// </summary>
    public Task<TEntity> QueryEntityByIdAsync(long? id, string errorMsg = "数据不存在");

    public Task<List<TEntity>> QueryByIdsAsync(List<long> ids);

    #endregion

    #region 保存或更新

    /// <summary>
    /// 设置操作人信息
    /// </summary>
    public void SetOperateInfo(TEntity entity);

    /// <summary>
    /// 保存或更新
    /// </summary>
    public Task<TEntity?> SaveOrUpdateAsync(TEntity entity, Func<TEntity, Task>? insertCallback,
        Func<TEntity, Task>? updateCallback = null,
        bool isIgnoreNull = true);

    public Task<TEntity?> SaveOrUpdateAsync(TEntity entity, bool isIgnoreNull = true);

    public Task<TEntity?> SaveOrUpdateAsync(TEntity entity, Func<SaveType, TEntity, Task>? saveCallback,
        bool isIgnoreNull = true);

    public Task<bool> InsertRangeAsync(List<TEntity> entities, bool isIgnoreNull);

    #endregion

    #region 删除

    public Task<bool> LogicDeleteAsync(long? id);

    #endregion
    
    #region 对比

    public (List<TEntity>, List<TEntity>, List<TEntity>) Compare(
        List<TEntity> newDtoList, List<TEntity> newList);

    public (List<TEntityDto>, List<TEntity>, List<TEntity>) Compare<TEntityDto>(
        List<TEntityDto> newDtoList, List<TEntity> newList)
        where TEntityDto : VinFrameworkBaseEntityDto;

    #endregion
}